<?php
 include        '../data/compound_dal.php';
 include_once   '../entity/compound_entity.php';

class CompoundMng
{
    function update($compoundEntity)
    {
        $compoundDAL = new CompoundDAL();
        return $compoundDAL->update($compoundEntity->id,
                                    $compoundEntity->rfqID,
                                    $compoundEntity->color,
                                    $compoundEntity->effect,
                                    $compoundEntity->application,
                                    $compoundEntity->shade,
                                    $compoundEntity->exactness,
                                    $compoundEntity->physicalProperty,
                                    $compoundEntity->translucency,
                                    $compoundEntity->colorReference,
                                    $compoundEntity->inTime,
                                    $compoundEntity->inUser);
    }

    function get($id)
    {
        $where  = " id = "  . $id;
        $compoundDAL = new CompoundDAL();
        if ($result = $compoundDAL->get($mysqli, $where)) {
            $row = $result->fetch_assoc();
            $compoundEntity = new CompoundEntity();
            foreach ($row as $key => $value) {
                $compoundEntity->$key = $value;
            }
            return $compoundEntity;
        } else {
            return false;
        }
    }

    function getAll($start, $pageSize, $sortField, $sortDirection)
    {
        $orderBy = "ORDER BY " . $sortField . " " . $sortDirection . " LIMIT " . $start . ", " . ($pageSize * ($start + 1));
        $compoundDAL = new CompoundDAL();
        if ($result = $compoundDAL->getAll($orderBy)) {
            while ($row = $result->fetch_array()) {
                $compoundList[] = $row;
            }
            return $compoundList;
        } else {
            return false;
        }
    }

    function delete($id)
    {
        $compoundDAL = new CompoundDAL();
        return $compoundDAL->delete($id);
    }
}
?>
